package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Collect;
import org.rcloud.io.model.Endorsement;
import org.rcloud.io.model.File;
import org.rcloud.io.service.CollectService;
import org.rcloud.io.service.EndorsementService;
import org.rcloud.io.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Administrator on 2019/2/26.
 */
@RestController
@RequestMapping(value = "/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    @Autowired
    private EndorsementService endorsementService;

    @Autowired
    private FileService fileService;

    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public int add(@RequestBody String params) {

//        System.out.println("======params======" + params);
        //添加收藏前先去收藏表查询下该用户有没有收藏过，根据userid和eid
        Collect collect1 = JSON.parseObject(params, Collect.class);
        Collect collect2 = (Collect) collectService.selectByPrimaryKey(collect1).getInfo();
        if (null != collect2) {//!collect2.getId().equals(null) ：这样的条件，第一次收藏会取不到getId()，判断会报错
            Collect collect3 = new Collect();
            if (collect2.getFlag().equals(1)){
                System.out.println("=======取消点赞！======");
                collect3.setFlag(0);
                collect3.setUserid(collect1.getUserid());
                collect3.setEid(collect1.getEid());
                if (Integer.valueOf(collectService.updateByPrimaryKeySelective(collect3).getInfo().toString()) == 1){
                    return 3;
                }
            }else{
                System.out.println("=======已取消点赞，进行再次点赞...======");
                //方式一：在原先的这条数据，只改flag
                collect3.setFlag(1);
                collect3.setUserid(collect1.getUserid());
                collect3.setEid(collect1.getEid());
                //方式二：覆盖原先该条数据的所有字段并更改flag
//                collect1.setFlag(1);
                if (Integer.valueOf(collectService.updateByPrimaryKeySelective(collect3).getInfo().toString()) == 1){
                    return 2;
                }
            }
            return Integer.valueOf(collectService.updateByPrimaryKeySelective(collect3).getInfo().toString());
        } else {
            System.out.println("=======未点赞，进行点赞...======");
            JSONObject object = JSON.parseObject(params);
            object.put("id", UUID.randomUUID());
            object.put("createtime", new Date());
            object.put("flag", 1);
            Collect collect = JSON.parseObject(object.toString(), Collect.class);
            return Integer.valueOf(collectService.insertSelective(collect).getInfo().toString());
        }
    }

    /*
    * 修改（包括取消收藏再收藏）
    *
    * */

    @RequestMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    public ResultVo update(@RequestBody String params) {
        Collect collect = JSON.parseObject(params, Collect.class);
        return collectService.updateByPrimaryKeySelective(collect);
    }

    /*
    * 删除
    *
    * */
    @RequestMapping(value = "/delet", produces = {"application/json;charset=UTF-8"})
    public ResultVo delet(@RequestBody String params) {
        Collect collect = JSON.parseObject(params, Collect.class);
        return collectService.deleteByPrimaryKey(collect);
    }

    /*
    * 查询
    *
    * */
    @RequestMapping(value = "/select", produces = {"application/json;charset=UTF-8"})
    public ResultVo select(@RequestBody String params) {
        Collect collect = JSON.parseObject(params, Collect.class);
        return collectService.selectByPrimaryKey(collect);
    }


    /*
    * 点赞记录
    *
    * */
    @RequestMapping(value = "/selectLike", produces = {"application/json;charset=UTF-8"})
    public ResultVo selectLike(@RequestBody String params) {//Integer比int更安全，查不出数据前者输出null,后者输出0
        Collect collect = JSON.parseObject(params, Collect.class);
        if (collectService.selectByLike(collect).getCode() == 0) {
            List<Collect> selectLikeList = collectService.selectByLike(collect).getInfo();
            return ResultVo.setOK(selectLikeList.size());
        } else {
            return ResultVo.setError(collectService.selectByLike(collect).getMsg());
        }


    }


    /*
   * 传个userid查询该用户所有的收藏eid，然后根据eid再获取每个id对应的代言家庭信息和对应的图片
   *
   * */
    @RequestMapping(value = "/selectEndorFile", produces = {"application/json;charset=UTF-8"})
    public JSONArray selectByEndorFile(@RequestBody String params) {//只传了个userid

        Collect collect = JSON.parseObject(params, Collect.class);
        List<Collect> collectList = collectService.selectByCollectEid(collect).getInfo();
        JSONArray jsonArray = new JSONArray();
        for (Collect collect1 : collectList) {
            Endorsement endorsement = (Endorsement) endorsementService.selectByendorsementid(collect1.getEid().toString()).getInfo();
            JSONObject jsonObject = (JSONObject) JSON.toJSON(endorsement);
            System.out.println("oooooooooooo:"+jsonObject);
            if(null != endorsement){
                File file = new File();
                file.setEid(endorsement.getEid());
                List<File> fileList_endor = fileService.selectByFileEndor(file).getInfo();//各代言家庭中对应的图片集合
                jsonObject.put("remark", fileList_endor);//json新建个键值对，key值存的是各代言家庭中对应的图片集合
                jsonArray.add(jsonObject);
            }
        }
        System.out.println("===cccccccc====:");
        return jsonArray;
    }


    /*
    * 分页查询（带参数）
    *
    * */
    @RequestMapping(value = "/selectByOther", produces = {"application/json;charset=UTF-8"})
    public PageInfo<Collect> selectByOther(@RequestBody String params) {
        JSONObject obj = JSON.parseObject(params);
        Map map = JSON.parseObject(obj.get("params").toString());
        int pageNum = Integer.parseInt(obj.get("pageNum").toString());//页数
        int pageSize = Integer.parseInt(obj.get("pageSize").toString());//查多少条
        PageInfo<Collect> PageInfo = collectService.selectByOther(pageNum, pageSize, map);
        return PageInfo;
    }

}
